home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / examples / ftpxfer < prev    next >
Encoding:
Text File  |  1994-02-08  |  6.2 KB  |  174 lines

  1. #!/usr/local/bin/icmake -qi
  2.  
  3. /***************************************************************************
  4.  
  5.     This file shows an example of a shell around ftp. I use this program a 
  6. lot in situations where I want to transfer a file from one unix host to 
  7. another, and when I know beforehand what file from what directory I want to 
  8. transfer. For the installation:
  9.     - copy this file to your personal bin directory, under the name
  10.       ftpxfer
  11.     - make the file executable: chmod +x ftpxfer
  12.     
  13.     The program will prompt for the user name and password to use for the 
  14. ftp transfer. If you often access one host with this program, and don't wont 
  15. to type the user/password all the time, you can do the follwing for bash:
  16.     > set FTPUSER my_login_name_on_that_host
  17.     > export FTPUSER
  18.     > set FTPASS my_password_on_that_host
  19.     > export FTPASS
  20. For tcsh, try:
  21.     > setenv FTPUSER my_login_name_on_that_host
  22.     > setenv FTPASS my_password_on_that_host
  23. Net result: this program won't prompt you for the strings, but will retrieve 
  24. them from the environment table. You can, of course, place these commands in 
  25. your .login or .tcshrc file, to be executed automatically during the login 
  26. procedure. But then it may be a good idea to make these files read/write 
  27. only for you and for nobody else (e.g., by: chmod 644 .login).
  28.  
  29.     The actual ftp transfer occurs using an intermediate temporary file, 
  30. TMPFILE in the below #define's. The user name and password _are_ stated in 
  31. that file, but this should not be a security risk. First, the file is 
  32. read/write for the user only, and for no-one else. Second, the file gets 
  33. deleted as soon as it's no longer needed. If you consider this feature still 
  34. a security hazard, take a valium and don't use this program.
  35.  
  36. *****************************************************************************/
  37.  
  38. // here's a couple of defines, no need to change them..
  39. #define VER         "1.02"
  40. #define YEARS         "1993"
  41.  
  42. // the following define controls ftp's `verbatim' mode, set it to "-v" 
  43. // if you want verbatim, or to "" if you don't
  44. #define VERBATIM    "-v"
  45.  
  46. list
  47.     envp;                    // environment strings
  48.  
  49. string
  50.     tmpfile,                    // temp file for ftp use
  51.     host,                    // host to transfer from/to
  52.     dir,                    // foreign directory
  53.     file,                    // local file
  54.     direction;                    // "get" or "put" file?
  55.     
  56. string getenv (string varname)            // purpose: returns setting
  57. {                        // of environment variable
  58.     int                        // 'varname'
  59.         i;
  60.         
  61.     for (i = 0; i < sizeof (envp); i += 2)    // loop thru envp...
  62.         if (element (i, envp) == varname)    // found varname?
  63.             return (element (i + 1, envp));    // yes -- return setting
  64.             
  65.     return ("");                // no -- return empty string
  66. }
  67.  
  68. void inittmp ()                    // purpose: initialize temp
  69. {                        // file
  70.  
  71.     if (exists (tmpfile))            // remove any old version
  72.         exec ("rm", tmpfile);            // if it exists
  73.         
  74.     exec ("touch", tmpfile);            // make empty file
  75.     exec ("chmod", "600", tmpfile);        // make it r/w only for user
  76. }
  77.     
  78. void process ()                    // purpose: do the actual 
  79. {                        // ftp transfer
  80.  
  81.     string
  82.         user,                    // user name on foreign host
  83.         password,                // password
  84.         foreignfile;                // full name of foreign file
  85.         
  86.     inittmp ();                    // make new temp file
  87.  
  88.     if (dir)                    // if foreign dir specified:
  89.     foreignfile = change_path (file, dir);    // use that
  90.     else if (get_path (file))            // if file spec has its own
  91.         foreignfile = file;            // directory: keep it
  92.     else                    // otherwise: use current
  93.         foreignfile = change_path (file,     // directory as dest dir
  94.             chdir ("."));
  95.             
  96.     if (! (user = getenv ("FTPUSER")) )        // get username from envp
  97.     {                        // or prompt for it
  98.         printf ("User name: ");
  99.         user = gets ();
  100.     }
  101.     if (! (password = getenv ("FTPASS")) )    // get passwd from envp
  102.     {                        // or prompt for it
  103.         printf ("Password : ");
  104.         password = gets ();
  105.     }
  106.     
  107.     fprintf (tmpfile,                 // write ftp login procedure
  108.         "open ", host, "\n",            // to tmpfile, followed
  109.         "user ", user, " ", password, "\n",    // by transfer commands
  110.         "binary\n",
  111.         direction, " ", file, " ", foreignfile, "\n",
  112.         "quit\n");
  113.     
  114.     exec (P_NOCHECK, "ftp", VERBATIM,         // do the ftp transfer
  115.         "-n -i", "< ", tmpfile);
  116.     exec (P_NOCHECK, "rm", tmpfile);        // remove temp file
  117. }    
  118.     
  119. void usage ()                    // purpose: print usage info
  120. {                        // and die
  121.     printf ("\n"
  122.             "ICCE Ftp-based File Transfer Shell  V", VER, "\n"
  123.             "Copyright (c) ICCE ", YEARS, ". All rights reserved.\n"
  124.             "\n"
  125.             "Usage: ftpxfer -p|-g host file [directory]\n"
  126.             "where:\n"
  127.             "       -p         : selects putting of file\n"
  128.             "       -g         : selects getting of file\n"
  129.             "       host       : host to put/get from/to\n"
  130.             "       file       : file to transfer\n"
  131.             "       directory  : optional directory at foreign host, if "
  132.                                     "not given:\n"
  133.         "                    directory in file argument is used, if not "
  134.                                     "present:\n"
  135.         "                    current directory is used for destination\n"
  136.             "Ftpxfer will use the environment variables FTPUSER and FTPASS "
  137.                                     "when available,\n"
  138.         "or will prompt for the user and password.\n"
  139.             "\n");
  140.     exit (1);
  141. }
  142.     
  143. void main (int argc, list argv, list evp)    // main function
  144. {
  145.     envp = evp;                    // store environment
  146.     echo (OFF);                    // no re-echoing of commands
  147.     tmpfile = "/tmp/ftpxfer."             // make temporary filename
  148.         + (string) getpid ();
  149.     
  150.     if (element (1, argv) == "-p")        // first argument: must
  151.         direction = "put";            // be -p or -g
  152.     else if (element (1, argv) == "-g")
  153.         direction = "get";
  154.     else
  155.         usage ();
  156.         
  157.     if (! (host = element (2, argv)) )        // second argument: must be
  158.         usage ();                // foreign host
  159.     if (! (file = element (3, argv)) )        // third argument: must be
  160.         usage ();                // file to transfer
  161.     if (direction == "put" && ! exists (file))    // if putting: file must
  162.     {                        // exist
  163.         printf ("File to put does not exist.\n");
  164.         exit (1);
  165.     }    
  166.     
  167.     dir = element (4, argv);            // fourth element: may be 
  168.                             // foreign directory
  169.     
  170.     process ();                    // hit it!
  171.         
  172.     exit (0);                    // exitstatus: success
  173. }
  174.